package multiplex.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;


public class TelaGerenciaDestaqueMarketing extends InternalFramePrincipal {
	private DualListBox meuDualList;
	
	public TelaGerenciaDestaqueMarketing(){
		super("Insere Destaque");
		
		Container conteudo = getContentPane();   
        conteudo.setLayout(new GridLayout(1,1,5,5)); 
        
        meuDualList = new TelaGerenciaDestaqueMarketing.DualListBox();
        
        conteudo.add(meuDualList);
	}
	
private class SortedListModel extends AbstractListModel{
	private SortedSet<Object> model;
	
	public SortedListModel(){
		model = new TreeSet<Object>();
	}
	
	@Override
	public Object getElementAt(int arg0) {
		// TODO Auto-generated method stub
		return model.toArray()[arg0];
	}

	@Override
	public int getSize() {
		// TODO Auto-generated method stub
		return model.size();
	}
	
	 public void add(Object element) {
		    if (model.add(element)) {
		      fireContentsChanged(this, 0, getSize());
		    }
		  }

		  public void addAll(Object elements[]) {
		    Collection<Object> c = Arrays.asList(elements);
		    model.addAll(c);
		    fireContentsChanged(this, 0, getSize());
		  }

		  public void clear() {
		    model.clear();
		    fireContentsChanged(this, 0, getSize());
		  }

		  public boolean contains(Object element) {
		    return model.contains(element);
		  }

		  public Object firstElement() {
		    return model.first();
		  }

		  public Iterator<Object> iterator() {
		    return model.iterator();
		  }

		  public Object lastElement() {
		    return model.last();
		  }

		  public boolean removeElement(Object element) {
		    boolean removed = model.remove(element);
		    if (removed) {
		      fireContentsChanged(this, 0, getSize());
		    }
		    return removed;
		  }
}

	class DualListBox extends JFrame{
		  private JList sourceList;

		  private SortedListModel sourceListModel;

		  private JList destList;

		  private SortedListModel destListModel;

		  private JButton addButton;

		  private JButton removeButton;

		  public DualListBox() {
		    initScreen();
		  }

		  public void clearSourceListModel() {
		    sourceListModel.clear();
		  }

		  public void clearDestinationListModel() {
		    destListModel.clear();
		  }

		  public void addSourceElements(ListModel newValue) {
		    fillListModel(sourceListModel, newValue);
		  }

		  public void setSourceElements(ListModel newValue) {
		    clearSourceListModel();
		    addSourceElements(newValue);
		  }

		  public void addDestinationElements(ListModel newValue) {
		    fillListModel(destListModel, newValue);
		  }

		  private void fillListModel(SortedListModel model, ListModel newValues) {
		    int size = newValues.getSize();
		    for (int i = 0; i < size; i++) {
		      model.add(newValues.getElementAt(i));
		    }
		  }

		  public void addSourceElements(Object newValue[]) {
		    fillListModel(sourceListModel, newValue);
		  }

		  public void setSourceElements(Object newValue[]) {
		    clearSourceListModel();
		    addSourceElements(newValue);
		  }

		  public void addDestinationElements(Object newValue[]) {
		    fillListModel(destListModel, newValue);
		  }

		  private void fillListModel(SortedListModel model, Object newValues[]) {
		    model.addAll(newValues);
		  }

		  private void clearSourceSelected() {
		    Object selected[] = sourceList.getSelectedValues();
		    for (int i = selected.length - 1; i >= 0; --i) {
		      sourceListModel.removeElement(selected[i]);
		    }
		    sourceList.getSelectionModel().clearSelection();
		  }

		  private void clearDestinationSelected() {
		    Object selected[] = destList.getSelectedValues();
		    for (int i = selected.length - 1; i >= 0; --i) {
		      destListModel.removeElement(selected[i]);
		    }
		    destList.getSelectionModel().clearSelection();
		  }

		  private void initScreen() {
		    setLayout(new GridLayout(0, 2));
		    sourceListModel = new SortedListModel();
		    sourceList = new JList(sourceListModel);

		    addButton = new JButton(">>");
		    addButton.addActionListener((ActionListener) new AddListener());
		    removeButton = new JButton("<<");
		    removeButton.addActionListener(new RemoveListener());

		    destListModel = new SortedListModel();
		    destList = new JList(destListModel);

		    JPanel leftPanel = new JPanel(new BorderLayout());
		    leftPanel.add(new JLabel("Available Elements:"), BorderLayout.NORTH);
		    leftPanel.add(new JScrollPane(sourceList), BorderLayout.CENTER);
		    leftPanel.add(addButton, BorderLayout.SOUTH);

		    JPanel rightPanel = new JPanel(new BorderLayout());

		    rightPanel.add(new JLabel("Selected Elements:"), BorderLayout.NORTH);
		    rightPanel.add(new JScrollPane(destList), BorderLayout.CENTER);
		    rightPanel.add(removeButton, BorderLayout.SOUTH);

		    add(leftPanel);
		    add(rightPanel);
		  }

		  private class AddListener implements ActionListener {
		    public void actionPerformed(ActionEvent e) {
		      Object selected[] = sourceList.getSelectedValues();
		      addDestinationElements(selected);
		      clearSourceSelected();
		    }

		
		  }

		  private class RemoveListener implements ActionListener {
		    public void actionPerformed(ActionEvent e) {
		      Object selected[] = destList.getSelectedValues();
		      addSourceElements(selected);
		      clearDestinationSelected();
		    }
		  }

	}
  }


